package com.magus.project.yddemo.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magus.framework.core.exception.app.ServiceException;
import com.magus.framework.core.repository.BaseRepository;
import com.magus.framework.core.service.BaseService;
import com.magus.project.yddemo.entity.Indicator;
import com.magus.project.yddemo.entity.IndicatorGroup;
import com.magus.project.yddemo.repository.IndicatorRepository;
import com.magus.project.yddemo.vobean.IndicatorVoBean;

/**
 * 指标 Service
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Service
@Transactional(readOnly = true)
public class IndicatorService extends BaseService<Indicator, String> {

	@Autowired
	private IndicatorRepository indicatorRepository;

	// @Autowired
	// private AppGroupService groupService;

	@Override
	protected BaseRepository<Indicator, String> getBaseRepository() {
		return this.indicatorRepository;
	}

	/**
	 * @throws ServiceException
	 *             如果KEY冲突则抛出异常
	 */
	@Override
	@Transactional
	public Indicator save(Indicator entity) {
		return indicatorRepository.save(entity);
	}

	@Transactional
	public void delete(List<Indicator> indicators) {
		for (Indicator entity : indicators) {
			indicatorRepository.delete(entity);
		}
	}
	public List<IndicatorVoBean> findIndicatorVoBeanByGrpup(IndicatorGroup group,String dimension){
		dimension=dimension==null?"":"%"+dimension+"%";
		List<Indicator> findByGroup = indicatorRepository.findByGroupAndDimensionLike(group,dimension);
		List<IndicatorVoBean> collect = findByGroup.stream()//
		.map(temp->{
			IndicatorVoBean voBean = new IndicatorVoBean();
			voBean.setName(temp.getName());
			voBean.setCode(temp.getCode());
			voBean.setUnit(temp.getUnit());
			voBean.setDimension(temp.getDimension());
			//voBean.setGroup(temp.getGroup());
			voBean.setId(temp.getId());
			
			return voBean;
		}).collect(Collectors.toList());
		
		
		return collect;
	}

}
